class Slider

    constructor: (options)->
        @$page = options.container || $('body .section')
        @$page = $(@$page) if (typeof @$page).toLowerCase() is 'string'
        @$container = @$page.parent()
        @$pageCount = @$page.length

        @animateTime = options.animateTime || 1000
        @pagination = options.pagination || true
        @easing = options.easing || 'ease'
        @loop = options.loop || false
        @keyboard = options.keyboard || false
        @direction = options.direction || 'landscape'

        @isLandscape = @direction is 'landscape'
        if @isLandscape
            $("body").css
                'overflow-x': 'hidden'
            $("body").addClass("landscape")
        else
            $("body").css
                'overflow-y': 'hidden'

        @init()

    init: ->
        return unless @$pageCount # 无分页
        @$container.css
            'position': 'absolute'
            'height': '100%'
            'width': '100%'
        @$page.css
            'position': 'absolute'
            'height': '100%'
            'width': '100%'

        for page,i in @$page
            if @isLandscape
                $(page).css
                    left: "#{i}00%"
                    top: "0"
            else
                $(page).css
                    left: "0"
                    top: "#{i}00%"

        @$container.css
            '-webkit-transition': "all #{@animateTime}ms #{@easing}"
            'transition': "all #{@animateTime}ms #{@easing}"

        @initPagination()

    initPagination: ->
        $pagination = $ "<div>",
            class: "slider-pagination"

        $page = $ "<div>",
            class: "slider-page"
        for page,i in @$page
            $p = $page.clone()
            $p.addClass("active") if i is 0
            $p.appendTo $pagination

        $pagination.insertAfter @$container
        $pagination.find('.slider-page').on 'click touchstart', (e)=>
            $this = $ e.currentTarget
            index = $this.index()
            $this.addClass("active").siblings().removeClass("active")
            if @isLandscape
                @$container.css
                    'transform': "translateX(-#{index}00%)";
            else
                @$container.css
                    'transform': "translateY(-#{index}00%)";

window["Slider"] = Slider

